<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>EQEmu</title>
    <link rel="stylesheet" type="text/css" href="style/style.css"/>
    <script src="scripts/jquery.js"></script>
    <script>
    <?
        $lname = $request->get("name", "ERROR");
    ?>
    <!--
    var idx = Math.floor((Math.random()*100000000));
    function GetStatusIdx() {
        return idx++;
    }

    function StatusTic() {
        $.getJSON("data/launcher_data.html?launcher="+<?print "\"$lname\"";?>+"&idx="+GetStatusIdx(),
        function(data) {
            $("#launcher_table").empty();
            var app_html = "<tr>";
            app_html += "<th scope=\"col\">Zone Name</th>";
            app_html += "<th scope=\"col\">State</th>";
            app_html += "<th scope=\"col\">Port</th>";
            app_html += "<th scope=\"col\">Start Count</th>";
            app_html += "<th scope=\"col\">Actions</th>";
            app_html += "</tr>";
            $("#launcher_table").append(app_html);
            for(var i = 0; i < data.zone_count; i++) {
                app_html = "<tr>";
                app_html += "<td><a href='zone.html?zone="+data.zones[i].name+"'>"+data.zones[i].name+"</a></td>";
                if(data.zones[i].up) {
                    app_html += "<td><font color='#009900'><b>UP</b></font></td>";
                } else {
                    app_html += "<td><font color='#CC0000'><b>DOWN</b></font></td>";
                }
                if(data.zones[i].port == 0) {
                    app_html += "<td>Dynamic</td>";
                } else {
                    app_html += "<td>"+data.zones[i].port+"</td>";
                }
                app_html += "<td>"+data.zones[i].starts+"</td>";
                app_html += "<td>";
                if(data.connected) {
                    app_html += "<a href='javascript:void(0)' onclick='RestartZone(\""+data.zones[i].name+"\")'>Restart</a> - ";
                    if(data.zones[i].up) {
                        app_html += "<a href='javascript:void(0)' onclick='StopZone(\""+data.zones[i].name+"\")'>Stop</a>";
                    } else {
                        app_html += "<a href='javascript:void(0)' onclick='StartZone(\""+data.zones[i].name+"\")'>Start</a>";
                    }
                    if(!data.zones[i].name.match(/dynamic/)) {
                        app_html += " - ";
                    }
                }
                if(!data.zones[i].name.match(/dynamic/)) {
                    app_html += "<a href='javascript:void(0)' onclick='RemoveZone(\""+data.zones[i].name+"\")'>Remove</a>";
                }
                app_html += "</td>";
                app_html += "</tr>";
                $("#launcher_table").append(app_html);
            }
        });
    }

    function ShowAddLauncher() {
        $("#add_launcher_link").hide();
        $("#min_add_launcher_link").show();
        $("#add_launcher_table").fadeIn("slow");
    }

    function HideAddLauncher() {
        $("#add_launcher_link").show();
        $("#min_add_launcher_link").hide();
        $("#add_launcher_table").fadeOut("slow");
    }

    function BootZone() {
        var elm = document.getElementById("short_name_field");
        var bzone = elm.value;
        elm.value = "";
        elm = document.getElementById("port_field");
        var bport = elm.value;
        elm.value = "0";

        elm = document.getElementById("boot_button_img");
        elm.style.display = "inline";

        elm = document.getElementById("boot_button");
        elm.className = "styled_button_disabled";
        $('#boot_button').unbind('click');

        $.getJSON("actions/launcher_action.html?action=boot&launcher_name="+<?print "\"$lname\"";?>+"&zone="+bzone+"&port="+bport+"&idx="+GetStatusIdx(),
        function(data) {
            $('#boot_button').bind('click', BootZone);
            elm = document.getElementById("boot_button");
            elm.className = "styled_button";

            elm = document.getElementById("boot_button_img");
            elm.style.display = "none";

            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function ChangeDynamicCount() {
        var elm = document.getElementById("dynamic_count_field");
        var dcount = elm.value;

        elm = document.getElementById("dynamic_count_button_img");
        elm.style.display = "inline";

        elm = document.getElementById("dynamic_count_button");
        elm.className = "styled_button_disabled";
        $('#dynamic_count_button').unbind('click');

        $.getJSON("actions/launcher_action.html?action=change_dynamic_count&launcher_name="+<?print "\"$lname\"";?>+"&dynamic_count="+dcount+"&idx="+GetStatusIdx(),
        function(data) {
            $('#dynamic_count_button').bind('click', ChangeDynamicCount);
            elm = document.getElementById("dynamic_count_button");
            elm.className = "styled_button";

            elm = document.getElementById("dynamic_count_button_img");
            elm.style.display = "none";

            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function RemoveZone(zone_name) {
        $.getJSON("actions/launcher_action.html?action=remove_zone&launcher_name="+<?print "\"$lname\"";?>+"&zone="+zone_name+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function RebootAll() {
        $.getJSON("actions/launcher_action.html?action=reboot_all&launcher_name="+<?print "\"$lname\"";?>+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function StopAll() {
        $.getJSON("actions/launcher_action.html?action=stop_all&launcher_name="+<?print "\"$lname\"";?>+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function StartAll() {
        $.getJSON("actions/launcher_action.html?action=start_all&launcher_name="+<?print "\"$lname\"";?>+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function RestartZone(zone) {
        $.getJSON("actions/launcher_action.html?action=restart_zone&launcher_name="+<?print "\"$lname\"";?>+"&zone="+zone+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function StartZone(zone) {
        $.getJSON("actions/launcher_action.html?action=start_zone&launcher_name="+<?print "\"$lname\"";?>+"&zone="+zone+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    function StopZone(zone) {
        $.getJSON("actions/launcher_action.html?action=stop_zone&launcher_name="+<?print "\"$lname\"";?>+"&zone="+zone+"&idx="+GetStatusIdx(),
        function(data) {
            if(data.status == 0) {
                alert(data.message);
            } else {
                StatusTic();
            }
        });
    }

    $(document).ready(function() {
        setInterval("StatusTic()", 4000);
        $('#boot_button').bind('click', BootZone);
        $('#dynamic_count_button').bind('click', ChangeDynamicCount);
    });
    -->
    </script>
</head>
<body>
    <div class="container">
        <?do("templates/scripts/menu.pl");?>
        <div id="main_content" class="main">
            <?
            $config = $EQW->GetLauncher($lname);
            if(!$config) {
                print "<h2>Unable to find launcher $lname</h2>";
            }
            ?>
            <div align="center">
                <a href="#" onclick='RebootAll()'>Reboot All Zones</a> -
                <a href="#" onclick='StopAll()'>Stop All Zones</a> -
                <a href="#" onclick='StartAll()'>Start All Zones</a>
            </div>
            </br>
            <div align="center">
                <a id='add_launcher_link' href="#" onclick='ShowAddLauncher()'>Add Zone</a>
                <a id='min_add_launcher_link' href="#" style="display: none;" onclick='HideAddLauncher()'>Minimize</a>
            </div>
            <table id='add_launcher_table' class='styled_table' width="70%" cellspacing="0" cellpadding="0" style="display: none">
                <tr>
                    <td style="text-align: left">
                    Add:
                    Zone Short Name: <input type='text' id='short_name_field'>
                    Port: <input type='text' id='port_field' value='0' size='5'>
                    <button id="boot_button" class="styled_button"><img id="boot_button_img" src="images/loading.gif" height="16" width="16" style='display: none;'>Boot</button>
                    </td>
                </tr>
                <tr>
                    <td style="text-align: left">
                    Change Dynamic Count: <input type='text' id='dynamic_count_field' value='<? print $config->GetDynamicCount(); ?>' size='5'>
                    <button id="dynamic_count_button" class="styled_button"><img id="dynamic_count_button_img" src="images/loading.gif" height="16" width="16" style='display: none;'>Change</button>
                    </td>
                </tr>
            </table>

            <table id='launcher_table' class='styled_table' width="70%" cellspacing="0" cellpadding="0">
              <tr>
                <th scope="col">Zone Name</th>
                <th scope="col">State</th>
                <th scope="col">Port</th>
                <th scope="col">Start Count</th>
                <th scope="col">Actions</th>
              </tr>
            <?
              my @zones = $config->ListZones();

              for my $zone (sort @zones) {
                    my $z = $config->GetZoneDetails($zone);
                    if(!$z) {
                        $zone->{name} = "ERROR: no launcher";
                    } elsif($z->{error}) {
                        $zone->{name} = "ERROR: $z->{error}";
                    }

                    print "<tr>\n";
                    print "\t<td><a href='zone.html?zone=$z->{name}'>$z->{name}</a></td>";
                    if($z->{up}) {
                        print "<td><font color='#009900'><b>UP</b></font></td>";
                    } else {
                        print "<td><font color='#CC0000'><b>DOWN</b></font></td>";
                    }
                    if($z->{port} == 0) {
                        print "<td>Dynamic</td>";
                    } else {
                        print "<td>$z->{port}</td>";
                    }
                    print "<td>$z->{starts}</td>";
                    print "<td>";
                    if($config->IsConnected()) {
                        print "<a href='javascript:void(0)' onclick='RestartZone(\"$z->{name}\")'>Restart</a> - ";
                        if($z->{up}) {
                            print "<a href='javascript:void(0)' onclick='StopZone(\"$z->{name}\")'>Stop</a>";
                        } else {
                            print "<a href='javascript:void(0)' onclick='StartZone(\"$z->{name}\")'>Start</a>";
                        }
                        if($z->{name} !~ /dynamic/) {
                            print " - ";
                        }
                    }
                    if($z->{name} !~ /dynamic/) {
                        print "<a href='javascript:void(0)' onclick='RemoveZone(\"$z->{name}\")'>Remove</a>";
                    }
                    print "</td>\n";
                    print "</tr>\n";
              }
            ?>
            </table>
        </div>
    </div>
    <div class="footer">
        &copy 2012 EQEmu. All rights reserved.
    </div>
</body>
</html>
